//--------------------------------------------------------------
//
// Qizhi Zhang 
// ezhung@gmail.com
// http://www.moorwind.com
//
//--------------------------------------------------------------
package reader.view
{
	import __AS3__.vec.Vector;
	
	import flash.utils.Dictionary;
	
	import org.puremvc.as3.multicore.interfaces.IMediator;
	import org.puremvc.as3.multicore.interfaces.INotification;
	
	import reader.ApplicationFacade;
	import reader.core.IGCAble;
	import reader.core.ReaderModelLocator;
	import reader.events.BlogSummaryEvent;
	import reader.model.IDataProxy;
	import reader.model.vo.BlogFetchVO;
	import reader.model.vo.BlogSourceVO;
	import reader.view.components.BlogSummary;

	public class BlogSummaryMediator extends BaseApplicationMediator implements IMediator, IGCAble
	{
		////////////////////////////////////////////////////////////////////////////////
		//public properties
		public static const NAME:String = "BlogSummaryMediator";
		
		public function get blogSummary():BlogSummary
		{
			return this.viewComponent as BlogSummary;
		}
		
		////////////////////////////////////////////////////////////////////////////////
		//private variables
		
		private var isAutoUpdateing:Boolean;
		private var dataProxy:IDataProxy;
		private var readerModelLocator:ReaderModelLocator = ReaderModelLocator.instance;
				
		//////////////////////////////////////////////////////////////////////////////
		//constructor
		public function BlogSummaryMediator(viewComponent:Object=null)
		{
			super(NAME, viewComponent);
			startUp();	
		}
		
		//////////////////////////////////////////////////////////////////////////////
		//methods
		
		public function startUp():void
		{
			dataProxy = facade.retrieveProxy(readerModelLocator.sqliteProxyFactory.dataProxyName) as IDataProxy;
			
			blogSummary.addEventListener(BlogSummaryEvent.FETCH_DETAILS, onFetchBlogDetails_handler, false, 0, true);
		}
		
		public function clearReference():void
		{
			dataProxy = null;
			blogSummary.removeEventListener(BlogSummaryEvent.FETCH_DETAILS, onFetchBlogDetails_handler);
			facade.removeMediator(NAME);
		}
		
		override public function listNotificationInterests():Vector.<String>
		{
			return Vector.<String>([ApplicationFacade.BLOG_SUMMARY_SHOW,
					ApplicationFacade.BLOG_SUMMARY_CREATE,
					ApplicationFacade.LOGOUT,
					ApplicationFacade.CREATE_LABEL_TREE
					]);
		}
		
		override public function handleNotification(notification:INotification):void
		{
			switch(notification.getName())
			{
				case ApplicationFacade.BLOG_SUMMARY_SHOW:
					if(!notification.getBody())
						dataProxy.getBlogSummary();
					break;
				case ApplicationFacade.BLOG_SUMMARY_CREATE:
					blogSummary.initSummary(notification.getBody() as Dictionary);
					break;
				case ApplicationFacade.LOGOUT:
					dataProxy.stopAutoUpdateBlogItem();
					clearReference();
					blogSummary.initSummary(null);
					break;
				case ApplicationFacade.CREATE_LABEL_TREE:
					if(!isAutoUpdateing)
					{
						isAutoUpdateing = true
						dataProxy.startAutoUpdateBlogItem();
					}	
					break;	
			}
		}
		
		//////////////////////////////////////////////////////////////////////////////
		//events
		
		private function onFetchBlogDetails_handler(e:BlogSummaryEvent):void
		{
			var labelArray:Vector.<int>;
			
			if(e.label)
			{
				labelArray = BlogSourceVO.getBlogSourceIdsByLabel(e.label, ReaderModelLocator.instance.blogSource);
			}
			
			var bf:BlogFetchVO = new BlogFetchVO(e.blogSource, labelArray);
			sendNotification(ApplicationFacade.BLOG_FETCH, bf);
			dataProxy.fetchBlogs(bf);
		}
		
		
	}
}